home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-08 | 16.3 KB | 654 lines | [TEXT/MPS ] |
- /*
- File: PanelEditorUtils.cpp
-
- Contains: PanelEditor utility functions & classes
-
- Written by: Steve Smith
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- */
-
- // -- Compiler/Preprocessor Switches --
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- // Exceptions define several important macros (ie. CHECKENV)
- // which are used in the SOM method dispatch glue. If Except.h
- // is not included early enough, exceptions may not be thrown
- // correctly when returning from a SOM method with "ev" parameter set.
- #include <Except.h>
- #endif
-
- // --- PanelEditor Includes ---
-
- #ifndef _PANELEDITORUTILS_
- #include "PanelEditorUtils.h"
- #endif
-
- #ifndef _PANELEDITORDEF_
- #include "PanelEditorDef.h"
- #endif
-
- #ifndef _SAMPLECOLLECTIONS_
- #include "SampleCollections.h"
- #endif
-
- #ifndef _PANELEDITORGLOBALS_
- #include "PanelEditorGlobals.h"
- #endif
-
- // --- OpenDoc Includes ---
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdDefs_defined
- #include <StdDefs.xh>
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include <Frame.xh>
- #endif
-
- #ifndef SOM_ODDraft_xh
- #include <Draft.xh>
- #endif
-
- #ifndef SOM_ODDocument_xh
- #include <Document.xh>
- #endif
-
- #ifndef SOM_ODContainer_xh
- #include <ODCtr.xh>
- #endif
-
- #ifndef SOM_ODNameSpaceManager_xh
- #include <NmSpcMg.xh>
- #endif
-
- #ifndef SOM_ODValueNameSpace_xh
- #include <ValueNS.xh>
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _BNDNSUTL_
- #include <BndNSUtl.h>
- #endif
-
- #ifndef _DOCUTILS_
- #include <DocUtils.h>
- #endif
-
- #ifndef _ODDEBUG_
- #include <ODDebug.h>
- #endif
-
- #ifndef _ODNEW_
- #include <ODNew.h>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _STDTYPIO_
- #include <StdTypIO.h>
- #endif
-
- #ifndef _STORUTIL_
- #include <StorUtil.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
- #ifndef _TEMPITER_
- #include <TempIter.h>
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- // --- Macintosh Includes ---
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __GXMATH__
- #include <GXMath.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #pragma segment PanelEditorUtilities
-
- //=========================================================================
- // Utility Classes
- //=========================================================================
-
- //-------------------------------------------------------------------------
- // CFrameProxy
- //-------------------------------------------------------------------------
-
- //-------------------------------------------------------------------------
- // CFrameProxy::SetFrame
- //
- // Description: The code will not affect the proxy fields unless it can
- // successfully acquire the incoming frame, it's id, and
- // the draft the frame lives in. If something goes wrong,
- // the proxy remains unchanged.
- //-------------------------------------------------------------------------
-
- void CFrameProxy::SetFrame(Environment* ev, ODFrame* frame)
- {
- ASSERT(frame!=kODNULL, kODErrIllegalNullFrameInput);
-
- ODID id;
- ODDraft* draft;
-
- // Using the temp object will cleanup the incoming
- // frame's refcount if something goes wrong.
- frame->Acquire(ev);
- TempODFrame tFrame = frame;
-
- // Perform all the operations that can fail first.
- TempODPart tPart = frame->AcquirePart(ev);
- id = frame->GetID(ev);
- draft = ODGetDraft(ev, tPart);
- ODReleaseObject(ev, fFrame);
-
- // If all went well, set the proxy fields.
- fFrame = tFrame.DontRelease();
- fID = id;
- fDraft = draft;
- }
-
- //-------------------------------------------------------------------------
- // CFrameProxy::GetFrame
- //-------------------------------------------------------------------------
-
- ODFrame* CFrameProxy::GetFrame(Environment* ev)
- {
- if ( fFrame == kODNULL )
- {
- TRY
- TempODFrame tFrame = fDraft->AcquireFrame(ev, fID);
- this->SetFrame(ev, tFrame);
- CATCH_ALL
- fFrame = kODNULL;
- fID = kODNULLID;
- ENDTRY
- }
- return fFrame;
- }
-
- //-------------------------------------------------------------------------
- // CFrameProxy::FrameIsLoaded
- //-------------------------------------------------------------------------
-
- ODBoolean CFrameProxy::FrameIsLoaded(Environment* ev)
- {
- ODBoolean loaded = (fFrame != kODNULL ||
- fDraft->IsValidID(ev, fID));
- return loaded;
- }
-
- //-------------------------------------------------------------------------
- // CFrameProxy::Purge
- //-------------------------------------------------------------------------
-
- void CFrameProxy::Purge(Environment* ev)
- {
- if ( fFrame != kODNULL )
- {
- fID = fFrame->GetID(ev);
- ODReleaseObject(ev,fFrame);
- }
- }
-
- //-------------------------------------------------------------------------
- // CDrawingText
- //-------------------------------------------------------------------------
-
- //-------------------------------------------------------------------------
- // CDrawingText::CDrawingText
- //-------------------------------------------------------------------------
-
- CDrawingText::CDrawingText()
- {
- GetPort(&port);
-
- // Save the text settings in the port.
- font = port->txFont;
- face = port->txFace;
- size = port->txSize;
-
- // Change the text settings for drawing the icon names.
- TextFont(gGlobals->fScriptFont);
- TextFace(normal);
- TextSize(gGlobals->fScriptFontSize);
- }
-
- //-------------------------------------------------------------------------
- // CDrawingText::CDrawingText
- //-------------------------------------------------------------------------
-
- CDrawingText::CDrawingText(ODIText* text)
- {
- GetPort(&port);
-
- // Save the text settings in the port.
- font = port->txFont;
- face = port->txFace;
- size = port->txSize;
-
- ODSShort fontNum;
- ODUShort fontSize;
-
- // Using the script code in the ODIText object, determine which
- // font and size to use for drawing.
- GetScriptFontSize(somGetGlobalEnvironment(), GetITextScriptCode(text),
- &fontNum, &fontSize);
-
- // Change the text settings for drawing the icon names.
- TextFont(fontNum);
- TextFace(normal);
- TextSize(fontSize);
- }
-
- //-------------------------------------------------------------------------
- // CDrawingText::~CDrawingText
- //-------------------------------------------------------------------------
-
- CDrawingText::~CDrawingText()
- {
- SetPort(port);
-
- // Restore the port text settings.
- TextFont(font);
- TextFace(face);
- TextSize(size);
- }
-
- //====================================================================
- // Utility Functions
- //====================================================================
-
- //--------------------------------------------------------------------
- // GetPartName
- //--------------------------------------------------------------------
-
- ODIText* GetPartName(Environment* ev, ODPart* part, ODType category)
- {
- ASSERT(part != kODNULL, kODErrIllegalNullPartInput);
-
- // Get the root part of the document.
- ODDraft* draft = ODGetDraft(ev,part);
- TempODPart rootPart = ODAcquireRootPartOfDraft(ev, draft);
-
- // Get the part name.
- // If we can't get a valid name for the part, we generate one using
- // the user visible category name of the part's category.
- TempODIText partName = ODGetITextProp(ev, part->GetStorageUnit(ev),
- kODPropName, kODMacIText, kODNULL);
-
- // If the part is the root of the document, return the name of the file.
- if ( ODObjectsAreEqual(ev, part, rootPart) )
- {
- ODContainer* container = draft->GetDocument(ev)->GetContainer(ev);
- TempPlatformFile file = GetPlatformFileFromContainer(ev, container);
-
- TempODIText fileName = file->GetName();
-
- // Test the file name against the part name. If the two are equivalent
- // (not equal), then use the part name.
- if ( !NamesAreEquivalent(ev, fileName, partName) )
- {
- // If the names are different, return the file name.
- // (This code transfers the ODIText object to the partName tempobj)
- DisposeIText(partName.DontDelete());
- partName = fileName.DontDelete();
- }
- }
- else
- {
- if ( (partName == kODNULL) || (GetITextStringLength(partName) == 0) )
- {
- ODIText* categoryName;
- ODNameSpaceManager* nsMgr = ODGetSession(ev,part)->GetNameSpaceManager(ev);
-
- // Get the category string from the category name space.
- if ( GetUserCatFromCat(nsMgr, category, &categoryName) )
- {
- // If we successfully retrieved the category user string, return it.
- // (This code transfers the ODIText object to the partName tempobj)
- DisposeIText(partName.DontDelete());
- partName = categoryName;
- }
- else
- {
- #if ODDebug
- // This should never happen. Check NMAP for errors.
- DebugStr("\pCategory NMAP bad, or Preferences corrupted.");
- #else
- THROW(kODErrInvalidNSType);
- #endif
- }
- }
- }
-
- return partName.DontDelete();
- }
-
- //--------------------------------------------------------------------
- // NamesAreEquivalent
- //--------------------------------------------------------------------
-
- ODBoolean NamesAreEquivalent(Environment* ev, ODIText* fileName,
- ODIText* partName)
- {
- Str255 fileStr;
- Str255 partStr;
-
- // If the strings are in different languages, we're done.
- if ( (GetITextScriptCode(fileName) != GetITextScriptCode(partName)) ||
- GetITextLangCode(fileName) != GetITextLangCode(partName) )
- return kODFalse;
-
- GetITextPString(fileName, fileStr);
- GetITextPString(partName, partStr);
-
- // If the string lengths are different, we're done.
- if ( fileStr[0] != partStr[0] )
- return kODFalse;
-
- // Return the Toolbox string equivalence test.
- return EqualString(fileStr,partStr,kODTrue,kODTrue);
- }
-
- //--------------------------------------------------------------------
- // GetEditorScriptLanguage
- //--------------------------------------------------------------------
-
- void GetEditorScriptLanguage(Environment* ev, ODScriptCode* script,
- ODLangCode* language)
- {
- long region;
-
- ODSLong rfRef;
- rfRef = BeginUsingLibraryResources();
- {
- Handle versHdl = Get1Resource('vers', 1);
-
- // Get the region code of the editor, otherwise use the
- // region code the of the primary system script.
-
- if ( versHdl )
- {
- region = (long)(*(VersRecHndl)versHdl)->countryCode;
- ReleaseResource(versHdl);
- }
- else
- {
- region = GetScriptManagerVariable(smRegionCode);
- }
-
- // Spanish & Japanese are not actually supported by the editor.
- // They are provided as examples of how to add recognition of
- // additional regions (see Script.h for region codes).
-
- switch ((short)region) {
- case verUS:
- *script = smRoman;
- *language = langEnglish;
- break;
- case verSpain:
- *script = smRoman;
- *language = langSpanish;
- break;
- case verJapan:
- *script = smJapanese;
- *language = langJapanese;
- break;
- default:
- *script = smRoman;
- *language = langEnglish;
- }
- }
- EndUsingLibraryResources(rfRef);
- }
-
- //--------------------------------------------------------------------
- // GetScriptFontSize
- //--------------------------------------------------------------------
-
- void GetScriptFontSize(Environment* ev, ODScriptCode script, ODSShort* fontNum,
- ODUShort* fontSize)
- {
- long result = GetScriptVariable(script, smScriptSmallFondSize);
-
- *fontNum = (ODSShort) HiWord(result);
- *fontSize = (ODUShort) LoWord(result);
- }
-
- //--------------------------------------------------------------------
- // FixedToIntRect
- //--------------------------------------------------------------------
-
- void FixedToIntRect(ODRect& fixedRect, Rect& intRect)
- {
- intRect.top = FixedToInt(fixedRect.top);
- intRect.left = FixedToInt(fixedRect.left);
- intRect.bottom = FixedToInt(fixedRect.bottom);
- intRect.right = FixedToInt(fixedRect.right);
- }
-
- //--------------------------------------------------------------------
- // IntToFixedRect
- //--------------------------------------------------------------------
-
- void IntToFixedRect(Rect& intRect, ODRect& fixedRect)
- {
- fixedRect.left = ff(intRect.left);
- fixedRect.top = ff(intRect.top);
- fixedRect.right = ff(intRect.right);
- fixedRect.bottom = ff(intRect.bottom);
- }
-
- //--------------------------------------------------------------------
- // LoadThumbnail
- //--------------------------------------------------------------------
-
- void LoadThumbnail(Environment* ev, Handle* thumbnail)
- {
- if ( *thumbnail ) return;
-
- ODSLong rfNum = BeginUsingLibraryResources();
- {
- *thumbnail = (Handle) GetPicture(kThumbnailPicture);
- }
- EndUsingLibraryResources(rfNum);
- }
-
- //--------------------------------------------------------------------
- // TilePartWindow
- //--------------------------------------------------------------------
-
- Rect TilePartWindow(Environment* ev, Rect* facetBounds, Rect* partWindowBounds)
- {
- const short kWindowTilingConst = 20;
- const short kLeftToRight = 0;
- const short kRightToLeft = -1;
-
- short direction;
-
- // Get the direction for the primary script system running on this machine.
- // (Right-to-Left or Left-to-Right)
- direction = GetSysDirection();
-
- // The child window should be tiled from the topLeft corner of the
- // active facet who's frame is being opened.
- if ( direction == kLeftToRight )
- {
- // Position the window rect at the top/left corner of the facet.
- OffsetRect(partWindowBounds, facetBounds->left, facetBounds->top);
- // Now tile the window rect down and to the right.
- OffsetRect(partWindowBounds, kWindowTilingConst, kWindowTilingConst);
- }
- // The child window should be tiled from the topRight corner of the
- // active facet who's frame is being opened.
- else if ( direction == kRightToLeft )
- {
- // Position the window rect at the top/right corner of the facet.
- OffsetRect(partWindowBounds, (partWindowBounds->right - facetBounds->right),
- facetBounds->top);
- // Now tile the window rect down and to the left.
- OffsetRect(partWindowBounds, -kWindowTilingConst, kWindowTilingConst);
- }
-
- return *partWindowBounds;
- }
-
- //--------------------------------------------------------------------
- // CountFacets
- //--------------------------------------------------------------------
-
- ODUShort CountFramesFacets(Environment* ev, ODFrame* frame)
- {
- ODUShort facetCount = 0;
- ODFacet* facet;
-
- TempODFrameFacetIterator ffiter(ev, frame);
- facet = ffiter.First();
- while ( ffiter.IsNotComplete() )
- {
- facetCount++;
- facet = ffiter.Next();
- }
-
- return facetCount;
- }
-
- //------------------------------------------------------------------------------
- // RemoveFacets (from BndNSUtl.cpp)
- //------------------------------------------------------------------------------
-
- void RemoveFacetsFromFrame(Environment* ev, ODFrame* frame)
- {
- CList* list = new CList;
- ODFacet* facet;
-
- TempODFrameFacetIterator fiter(ev, frame);
- for ( facet = fiter.First(); fiter.IsNotComplete();
- facet = fiter.Next() )
- {
- list->Add((ODPtr)facet);
- }
-
- CListIterator liter(list);
- for ( facet = (ODFacet*)liter.First();
- liter.IsNotComplete();
- facet = (ODFacet*)liter.Next() )
- {
- liter.RemoveCurrent();
-
- facet->GetContainingFacet(ev)->RemoveFacet(ev, facet);
- delete facet;
- }
- }
-
- //------------------------------------------------------------------------------
- // GetQDFrameBounds
- //------------------------------------------------------------------------------
-
- void GetQDFrameBounds(Environment* ev, ODFrame* frame, Rect* bounds)
- {
- ODRect boundingBox;
-
- TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);
- frameShape->GetBoundingBox(ev, &boundingBox);
-
- FixedToIntRect(boundingBox, *bounds);
- }
-
- //-------------------------------------------------------------------------
- // ScrollProc
- //-------------------------------------------------------------------------
-
- extern pascal void ScrollProc(ControlHandle control, short partCode)
- {
- short delta;
-
- if (partCode == 0)
- return;
-
- ScrollDataRec* sData = (ScrollDataRec*)(**control).contrlRfCon;
-
- switch(partCode) {
- case kControlUpButtonPart:
- delta = -1;
- break;
- case kControlDownButtonPart:
- delta = 1;
- break;
- case kControlPageUpPart:
- delta = -5;
- break;
- case kControlPageDownPart:
- delta = 5;
- break;
- default:
- delta = 0;
- }
-
- short vScroll = GetControlValue(control);
-
- SetControlValue(control, vScroll + delta);
-
- vScroll = ((GetControlValue(control) - vScroll) * kListItemHeight);
-
- // Some movement occurred.
- if ( vScroll != 0 )
- {
- ODFrame* frame = sData->frame;
- TempODTransform intTransform = frame->AcquireInternalTransform(sData->ev, kODNULL);
-
- Point offset = intTransform->GetQDOffset(sData->ev);
- offset.v -= vScroll;
- intTransform->SetQDOffset(sData->ev, &offset);
-
- Rect sr;
- GetQDFrameBounds(sData->ev, frame, &sr);
- InsetRect(&sr,1,1);
- sr.right -= 15;
-
- RgnHandle ur = NewRgn();
- ScrollRect(&sr, 0, -vScroll, ur);
-
- TempODShape us = frame->CreateShape(sData->ev);
- us->SetQDRegion(sData->ev, ur);
-
- frame->Invalidate(sData->ev, us, kODNULL);
- frame->ChangeInternalTransform(sData->ev, intTransform, kODNULL);
- }
- }
-
-
-
-
-
-
-
-